package com.zhou.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.entity.Account;
import com.zhou.entity.Outbound;
import com.zhou.entity.Process;
import com.zhou.entity.WareHouse;
import com.zhou.mapper.AdminMapper;
import com.zhou.mapper.OutboundMapper;
import com.zhou.mapper.ProcessMapper;
import com.zhou.mapper.WareHouseMapper;
import com.zhou.service.ProcessService;
import com.zhou.utis.booleanUtils.BooleanEaun;
import com.zhou.utis.threadLocal.ThreadLocalUtil;
import com.zhou.vo.processVo.InsertProcessVO;
import com.zhou.vo.processVo.UpdateProcessVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ProcessServiceImpl implements ProcessService {
    @Resource
    private ProcessMapper processMapper;
    @Resource
    private WareHouseMapper wareHouseMapper;
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private OutboundMapper outboundMapper;
    /**
     * 新增申请
     * @param vo
     * @return
     */
    @Override
    public String add(InsertProcessVO vo) {
        if (StringUtils.isEmpty(vo)){
            throw new RuntimeException("参数不能为空，请重试");
        }
        //通过工具类获取到用户id
        String id = ThreadLocalUtil.getId();
        if (StringUtils.isEmpty(id)){
            throw new RuntimeException("用户id没有取到，请重试");
        }
        Process process=new Process();
        BeanUtils.copyProperties(vo,process);
        process.setApplicantId(id);
        process.setProcessId(UUID.fastUUID().toString());
        //查询仓库的数据
        LambdaQueryWrapper<WareHouse> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(WareHouse::getMaterialName,vo.getMaterialName());
        wrapper.eq(WareHouse::getDelFlag, BooleanEaun.FLASE.getResultValue());
        WareHouse wareHouse = wareHouseMapper.selectOne(wrapper);
        if (StringUtils.isEmpty(wareHouse)){
            throw new RuntimeException("仓库没有了，请联系管理员添加");
        }
        process.setWareHouseId(wareHouse.getWareHouseId());
        //新增申请
        int insert = processMapper.insert(process);
        if (insert==0){
            throw new RuntimeException("新增申请失败，请重试");
        }
        return "申请成功，请等待审核";
    }

    /**
     * 审核
     * @return
     */
    @Override
    public String update(String processId) {
        //审核需要对仓库的库存进行判断是否能通过审核
        if (StringUtils.isEmpty(processId)){
            throw new RuntimeException("参数不能传空，请重试");
        }
        //查询是否存在这条申请记录
        LambdaQueryWrapper<Process> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Process::getDelFlag,BooleanEaun.FLASE.getResultValue());
        wrapper.eq(Process::getProcessId,processId);
        Process process = processMapper.selectOne(wrapper);
        if (StringUtils.isEmpty(process)){
            throw new RuntimeException("没有查到该条申请记录，请重试");
        }
        //进行审核，需判断仓库是否有所需的数量
        LambdaQueryWrapper<WareHouse> wrapper1=new LambdaQueryWrapper<>();
        wrapper1.eq(WareHouse::getDelFlag,BooleanEaun.FLASE.getResultValue());
        wrapper1.eq(WareHouse::getWareHouseId,process.getWareHouseId());
        WareHouse wareHouse = wareHouseMapper.selectOne(wrapper1);
        if (StringUtils.isEmpty(wareHouse)){
            throw new RuntimeException("仓库没有查到数据，请重试");
        }
        //进行数量对比
        Integer materialNumber = wareHouse.getMaterialNumber();
        Integer materialNumber1 = process.getMaterialNumber();
        if (materialNumber-materialNumber1<0){
            process.setStatus(2);
            process.setReason("仓库没有足够的数量，请联系管理员");
            return "审核失败";
        }else {
            process.setStatus(1);
        }
        //如果通过，就修改数据，并新增出库记录
         wareHouse.setMaterialNumber(materialNumber-materialNumber1);
        int insert = wareHouseMapper.updateById(wareHouse);
        if (insert==0){
            throw new RuntimeException("仓库数据更新失败，请重试");
        }
        Date date=new Date();
        process.setProcessTime(date);
        //需要去查登录的账户是谁的
        String id = ThreadLocalUtil.getId();
        Account account = adminMapper.selectById(id);
        if (StringUtils.isEmpty(account)){
            throw new RuntimeException("没有该用户，请重试");
        }
        process.setProcessPeople(account.getName());
        int i = processMapper.updateById(process);
        if (i==0){
            throw new RuntimeException("审核申请修改失败，请重试");
        }
        Outbound outbound=new Outbound();
        outbound.setOutboundId(UUID.fastUUID().toString());
        outbound.setOutboundNumber(process.getMaterialNumber());
        outbound.setOutboundTime(date);
        outbound.setMaterialName(process.getMaterialName());
        outbound.setMaterialType(wareHouse.getMaterialType());
        outbound.setDestination(process.getDestination());
        int insert1 = outboundMapper.insert(outbound);
        if (insert1==0){
            throw new RuntimeException("出库新增失败，请重试");
        }
        return "审核通过";
    }

    /**
     * 根据id查询
     * @param id 申请id
     * @return
     */
    @Override
    public Process getById(String id) {
        //判空
        if (StringUtils.isEmpty(id)){
            throw new RuntimeException("参数不能为空，请重试");
        }
        Process process = processMapper.selectById(id);
        if (StringUtils.isEmpty(process)){
            throw new RuntimeException("没有查到改条数据，请重试");
        }
        if (process.getDelFlag().equals(BooleanEaun.TURE.getResultValue())){
            throw new RuntimeException("该条物资已经被删除了，请重试");
        }

        return process;
    }



    /**
     * 申请修改
     * @param vo
     * @return
     */
    @Override
    public String updateOne(UpdateProcessVO vo) {
        //判空
        if(StringUtils.isEmpty(vo)){
            throw new RuntimeException("参数不能为空，请重试");
        }
        Process process1 = processMapper.selectById(vo.getProcessId());
        if (StringUtils.isEmpty(process1)){
            throw new RuntimeException("没有该条数据，请重试");
        }
        if (process1.getStatus().equals(BooleanEaun.TURE.getResultValue())){
            throw new RuntimeException("该申请已经审核通过了不能再修改了");
        }
        Process process=new Process();
        //将传进来的数据进行复制
        BeanUtils.copyProperties(vo,process);
        int i = processMapper.updateById(process);
        if (i==0){
            throw new RuntimeException("修改失败，请重试");
        }
        return "成功";
    }
    /**
     * 分页查询
     * @param page 1
     * @param pageSize 10
     * @return
     */
    @Override
    public List<Process> getAll(Integer page, Integer pageSize) {
        IPage<Process> page1=new Page<>(page,pageSize);
        LambdaQueryWrapper<Process> wrapper=new LambdaQueryWrapper<>();
        List<Process> records = processMapper.selectPage(page1, wrapper).getRecords();
        if (CollectionUtils.isEmpty(records)){
            throw new RuntimeException("没有查到数据，请重试");
        }
        return records;
    }
}
